package com.business.action.platform;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ClassUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.api.annotation.InterfaceClass;
import net.sf.rose.api.annotation.InterfaceMethod;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.redis.RedisClient;
import net.sf.rose.redis.RedisPoolUtils;
import net.sf.rose.util.BeanWrapper;
import net.sf.rose.web.util.WebUtils;

/**
 * 
 * @author fengjian
 * @version 创建时间：2018年7月15日
 * 基本数据库单表的操作接口类
 */
@Controller
@RequestMapping("/com/bean")
@InterfaceClass(title = "通用数据库表操作接口", description = "通用数据库表操作接口", sequence = 1003)
public class BasicBeanAction {

    /**
     * 列表查询
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/list.do")
	@InterfaceMethod(title = "列表查询", description = "根据参数列表查询数据库表数据", sequence = 1)
	public PageBean list(HttpServletRequest request, Service service, String className, String orderBy, boolean useCache) throws Exception {
		PageBean page = null;
		// 如果启用了缓存，先从缓存找
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			page = c.get(PageBean.class, className, "list");
			if (page != null) {
				return page;
			}
		}
		page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ClassUtils.getClass(className));
		query.createSql(WebUtils.getRequestData(request));
		if (orderBy != null) {
			query.addSql(" " + orderBy);
		}
		page.setData(dao.list());
		// 如果启用了缓存，将数据保存至缓存中
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			c.set(className, "list", page);
		}
		return page;
	}

    /**
     * 分页列表查询
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/page.do")
	@InterfaceMethod(title = "分页查询", description = "根据参数列表查询数据库表数据", sequence = 2)
	public PageBean page(HttpServletRequest request, Service service, String className, String orderBy, boolean useCache) throws Exception {
		PageBean page = WebUtils.getPageBean(request);
		// 如果启用了缓存，先从缓存找
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			page = c.get(PageBean.class, className, "page_" + page.getPageIndex());
			if (page != null) {
				return page;
			}
		}
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(ClassUtils.getClass(className));
		String tableName = request.getParameter("tableName");
		if (tableName != null) {
			query.setTableName(tableName);
		}
		query.createSql(WebUtils.getRequestData(request));
		if (orderBy != null) {
			query.addOrderby(orderBy);
		}
		dao.list();
		// 如果启用了缓存，将数据保存至缓存中
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			c.set(className, "page_" + page.getPageIndex(), page);
		}
		return page;
	}

    /**
     * 根据表的主键查询
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/getByID.do")
	@InterfaceMethod(title = "记录查询", description = "根据主键查询指定的记录", sequence = 3)
	public PageBean get(Service service, String className, String id, boolean useCache) throws Exception {
		PageBean page = null;
		// 如果启用了缓存，先从缓存找
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			page = c.get(PageBean.class, className, "get_" + id);
			if (page != null) {
				return page;
			}
		}
		page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ClassUtils.getClass(className));
		query.createSql(id);
		page.setData(dao.get());
		// 如果启用了缓存，将数据保存至缓存中
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			c.set(className, "get_" + id, page);
		}
		return page;
	}

    /**
     * 根据表的某个键值对查询
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/get.do")
	@InterfaceMethod(title = "记录查询", description = "根据指定的某个字段的值查询指定的某条记录", sequence = 3)
	public PageBean get(Service service, String className, String key, String value, boolean useCache) throws Exception {
		PageBean page = null;
		// 如果启用了缓存，先从缓存找
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			page = c.get(PageBean.class, className, "get_" + key + '_' + value);
			if (page != null) {
				return page;
			}
		}
		page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ClassUtils.getClass(className));
		query.createSql(key, value);
		page.setData(dao.get());
		// 如果启用了缓存，将数据保存至缓存中
		if (useCache && RedisPoolUtils.inUse()) {
			RedisClient c = new RedisClient();
			c.set(className, "get_" + key + '_' + value, page);
		}
		return page;
	}

    /**
     * 保存表数据
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/save.do")
	@InterfaceMethod(title = "保存记录", description = "保存记录", sequence = 4)
	public PageBean save(HttpServletRequest request, Service service, String className) throws Exception {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		Class<?> clazz = ClassUtils.getClass(className);
		Object bean = clazz.newInstance();
		query.setEntityClass(clazz);
		BeanWrapper bw = new BeanWrapper();
		bw.setWrappedInstance(bean);
		Map<String, Object> data = WebUtils.getRequestData(request);
		for(String key : data.keySet()){
			bw.set(key, data.get(key));
		}
		query.createSaveSql(bean);
		if (dao.update() == 0) {
			page.setSuccess(false);
			page.setMessage("数据库表保存失败！");
		} else {
			// 如果启用了缓存，删除缓存数据以更新
			if (RedisPoolUtils.inUse()) {
				RedisClient c = new RedisClient();
				c.hdelAll(className);
			}
		}
		return page;
	}

    /**
     * 插入表数据
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/insert.do")
	@InterfaceMethod(title = "保存记录", description = "保存记录", sequence = 4)
	public PageBean insert(HttpServletRequest request, Service service, String className) throws Exception {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		Class<?> clazz = ClassUtils.getClass(className);
		Object bean = clazz.newInstance();
		query.setEntityClass(clazz);
		BeanWrapper bw = new BeanWrapper();
		bw.setWrappedInstance(bean);
		Map<String, Object> data = WebUtils.getRequestData(request);
		for(String key : data.keySet()){
			bw.set(key, data.get(key));
		}
		query.createInsertSql(bean);
		if (dao.update() == 0) {
			page.setSuccess(false);
			page.setMessage("数据库表保存失败！");
		} else {
			// 如果启用了缓存，删除缓存数据以更新
			if (RedisPoolUtils.inUse()) {
				RedisClient c = new RedisClient();
				c.hdelAll(className);
			}
		}
		return page;
	}

    /**
     * 删除表数据
     * @throws Exception 
     */
	@ResponseBody
	@RequestMapping("/delete.do")
	@InterfaceMethod(title = "删除记录", description = "根据指定的表名和主键删除记录", sequence = 6)
	public PageBean delete(Service service, String className, String id, String updateIndexs) throws Exception {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		Class<?> clazz = ClassUtils.getClass(className);
		query.createDeleteSql(clazz, id);
		if (dao.update() == 0) {
			page.setSuccess(false);
			page.setMessage("数据库表没有一条数据被删除！");
		} else {
			// 如果启用了缓存，删除缓存数据以更新
			if (RedisPoolUtils.inUse()) {
				RedisClient c = new RedisClient();
				c.hdelAll(className);
			}
		}
		return page;
	}

}
